﻿Module Module1
    ' Module1.bas 
    ' Registry 
    Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByVal phkResult As Long) As Long
    Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByVal lpType As Long, ByVal lpData As String, ByVal lpcbData As Long) As Long
    Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByVal lpType As Long, ByVal lpData As Long, ByVal lpcbData As Long) As Long

    Const KEY_QUERY_VALUE = &H1
    Const HKEY_CLASSES_ROOT = &H80000000
    Const HKEY_CURRENT_USER = &H80000001
    Const HKEY_LOCAL_MACHINE = &H80000002
    Const HKEY_USERS = &H80000003

    Public Function GetRegStringValue$(ByVal Where As String, ByVal sKeyName$, ByVal sValueName$)
        Dim lRetVal As Long         'result of the API functions 
        Dim hKey As Long         'handle of opened key 

        lRetVal = RegOpenKeyEx(Where, sKeyName$, 0, KEY_QUERY_VALUE, hKey)

        Dim cch As Long
        Dim lrc As Long
        Dim lType As Long
        Dim sValue As String

        lrc = RegQueryValueExNULL(hKey, sValueName, 0&, lType, 0&, cch)
        GetRegStringValue$ = Space$(cch)
        lrc = RegQueryValueExString(hKey, sValueName, 0&, lType, GetRegStringValue$, cch)
        If lrc = 0 Then GetRegStringValue$ = Left$(GetRegStringValue$, cch - 1)

        RegCloseKey(hKey)
    End Function


End Module
